package com.xnyzc.lhy.didi.service.impl.other;

import com.alibaba.fastjson.JSONObject;
import com.xnyzc.lhy.common.component.didi.DiDiUrlConstant;
import com.xnyzc.lhy.common.entity.DiDiRv;
import com.xnyzc.lhy.common.exception.DiDiException;
import com.xnyzc.lhy.common.penum.EDiDiErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.RequestMethodUtil;
import com.xnyzc.lhy.common.util.retry.RetryUtil;
import com.xnyzc.lhy.common.util.sign.AttestationTool;
import com.xnyzc.lhy.didi.entity.param.other.InvalidNotifyParam;
import com.xnyzc.lhy.didi.entity.param.other.OtherOpenPidParam;
import com.xnyzc.lhy.didi.entity.param.other.ResponseRateParam;
import com.xnyzc.lhy.didi.entity.results.didi.other.OtherOpenPidResults;
import com.xnyzc.lhy.didi.entity.results.other.ResponseRateResults;
import com.xnyzc.lhy.didi.service.impl.order.OrderCorrelationServiceImpl;
import com.xnyzc.lhy.didi.service.other.OtherService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * 其它
 * </p>
 *
 * @author wangshuo
 * @since 2019-10-07
 */
@Service
@Slf4j
public class OtherServiceImpl implements OtherService {

    @Autowired
    private OrderCorrelationServiceImpl orderCorrelationServiceImpl;

    /**
     * 乘客id查询
     *
     * @param param 入参实体
     * @return
     */
    @Override
    public DiDiRv getOpenPid(OtherOpenPidParam param) {
        OtherOpenPidResults results = new OtherOpenPidResults();
        if (StringUtils.isBlank(param.getPassenger_phone())) {
            return DiDiRv.wrap(EDiDiErrorCode.parameterError, results);
        }
        String url = DiDiUrlConstant.didiUrl + DiDiUrlConstant.GET_OPEN_PID_API;
        Date date = new Date();
        String timestamp = String.valueOf(date.getTime() / 1000);
        param.setTimestamp(timestamp);
        param.setApp_id(DiDiUrlConstant.didiAppId);
        String signParameter = AttestationTool.getSign(param, DiDiUrlConstant.X_WWW_FORM_URLENCODED);
        String data = null;
        try {
            data = RetryUtil.retryRequestMethodUtil(signParameter, url, null, null, null);
        } catch (Exception e) {
            log.error("访问滴滴乘客ID查询接口失败: {}",e);
        }
        //如果请求滴滴失败data为空字符串
        if (CheckUtil.strIsEmpty(data)) {
            throw DiDiException.create(EDiDiErrorCode.httpRequestDidiUrlError);
        }
        JSONObject jsonObject = JSONObject.parseObject(data);
        if (jsonObject.get(DiDiUrlConstant.ERRNO).equals(EDiDiErrorCode.success.getValue())) {
            // 成功
            JSONObject dataObject = jsonObject.getJSONObject("data");
            if (CheckUtil.objIsNotEmpty(dataObject)) {
                results.setOpen_pid(dataObject.getString("open_pid"));
            }
        }
        return DiDiRv.wrap(EDiDiErrorCode.success, results);
    }

    /**
     * 发票冲红回调
     *
     * @param param 入参实体
     * @return
     */
    @Override
    public DiDiRv getInvalidNotify(InvalidNotifyParam param) {
        if (StringUtils.isBlank(param.getApplyTime()) || StringUtils.isBlank(param.getDiscard_id())) {
            return DiDiRv.wrapError(EDiDiErrorCode.parameterError);
        }
        String url = DiDiUrlConstant.didiUrl + DiDiUrlConstant.INVALID_NOTIFY_API + "?case_id=1&sandbox_token=Rr%2F99inHv9m2j040Vz9g6UXj2OGNspS0SDTsu7bMI7RcwWGYUJaEK1l3jdPBZjjT";
        param.setTimestamp(String.valueOf(System.currentTimeMillis()));
        String signParameter = AttestationTool.getSign(param, DiDiUrlConstant.X_WWW_FORM_URLENCODED);
        String data = RequestMethodUtil.requestMethod(signParameter, url);
        JSONObject jsonObject = JSONObject.parseObject(data);
        if (jsonObject.get(DiDiUrlConstant.ERRNO).equals(EDiDiErrorCode.success.getValue())) {
            // 成功
        }
        return DiDiRv.wrapSuccess(EDiDiErrorCode.success);
    }

    /**
     * 应答率
     *
     * @param param 入参实体
     * @return
     */
    @Override
    public DiDiRv getResponseRate(ResponseRateParam param) {
        ResponseRateResults results = new ResponseRateResults();
        if (StringUtils.isBlank(param.getFrom_lat()) || StringUtils.isBlank(param.getFrom_lng())) {
            return DiDiRv.wrap(EDiDiErrorCode.parameterError, results);
        }
        results.setAccept_rate(new BigDecimal(0));
        return DiDiRv.wrap(EDiDiErrorCode.success, results);
    }

}
